﻿using IRepositoryBll;
using IRepositoryDal;
using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryBll
{
   public  class WorkFlow_ModelBll : BaseDeleteBll<WorkFlow_Model>, IWorkFlow_ModelBll
    {
        private IWorkFlow_ModelDal _workFlow_ModelDal;
        public WorkFlow_ModelBll(IWorkFlow_ModelDal workFlow_ModelDal):base(workFlow_ModelDal)
        {
            _workFlow_ModelDal = workFlow_ModelDal;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public object GetWorkFlow_ModelListByPage(int page, int limit, out int count, string title)
        {
            //获取工作流模板表信息集合
            var workFlow_Model = _workFlow_ModelDal.GetEntitiesDb().Where((u => u.IsDelete == false));

            if (!string.IsNullOrEmpty(title))
            {
                workFlow_Model = workFlow_Model.Where(u => u.Description.Contains(title));

            }
            //统计数据数量
            count = workFlow_Model.Count();
            workFlow_Model = workFlow_Model.Skip((page - 1) * limit).Take(limit);
            var list = workFlow_Model.ToList().Select(u =>
            {
                return new
                {
                    u.ID,
                    u.Description,
                    u.Title,
                    CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                };
            });

            return list;
        }

        /// <summary>
        /// 获取流程模板下拉选数据
        /// </summary>
        /// <returns></returns>
        public object GetWorkFlow_ModelSelectOption()
        {
           
                //linq
                var title = _workFlow_ModelDal.GetEntitiesDb().Select(d => new
                {
                    d.ID,
                    d.Title
                }).ToList();
                return title;
            
        }



        /// <summary>
        /// 更新修改工作模板信息
        /// </summary>
        /// <param name="iD"></param>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public bool UpdateWorkFlow_Model(string workFlow_ModelId, string title, string description)
        {
            WorkFlow_Model workFlow_Model = _workFlow_ModelDal.GetEntity(workFlow_ModelId);
            if (workFlow_Model != null)
            {
                workFlow_Model.Description = description;
                workFlow_Model.Title = title;


                return _workFlow_ModelDal.Update(workFlow_Model);
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新工作流模板表
        /// </summary>
        /// <param name="workFlow_Model"></param>
        /// <returns></returns>
        public bool UpdateWorkFlow_Model(WorkFlow_Model workFlow_Model)
        {
            return _workFlow_ModelDal.Update(workFlow_Model);
        }
    }
}
